/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.mobilface.mingle;

import java.io.IOException;
import javax.microedition.lcdui.Image;
import javax.microedition.midlet.MIDlet;
import net.ubinow.mflex.baseui.AbsController;
import net.ubinow.mflex.baseui.ExpandableWidgetContainer;
import net.ubinow.mflex.baseui.ICommandBar;
import net.ubinow.mflex.baseui.WFont;
import net.ubinow.mflex.data.IDataItem;
import net.ubinow.mflex.data.SimpleDataItem;
import net.ubinow.mflex.events.Event;
import net.ubinow.mflex.events.IEventSubscriber;
import net.ubinow.mflex.util.Logging;
import net.ubinow.mflex.util.MiscUtil;
import net.ubinow.mflex.widgets.DialogView;
import net.ubinow.mflex.widgets.StandardCmdBar;
import net.ubinow.mflex.widgets.TabView;
import net.ubinow.mflex.widgets.WAjaxTextBox;
import net.ubinow.mflex.widgets.WButton;
import net.ubinow.mflex.widgets.WDropDown;
import net.ubinow.mflex.widgets.WLabel;
import net.ubinow.mflex.widgets.WList;
import net.ubinow.mflex.widgets.WTextBox;

/**
 *
 * @author josh.newnham
 */
public class Controller extends AbsController implements Constants, IEventSubscriber{

    private Image[] _imgs = null;
    private ICommandBar _cmdBar = null;
    private WFont _whiteFont = null;
    private WFont _smallBlackFont = null;
    private WFont _smallWhiteFont = null;
    
    public Controller( MIDlet midlet, Image shadingTile, Image loadingImage, int loadingImageW,
            int loadingImageH, int loadingAnimFrameTime, int screenWidth, int screenHeight ) {
        
        super( midlet, shadingTile, loadingImage, loadingImageW, loadingImageH,
                loadingAnimFrameTime, screenWidth, screenHeight );
    }

    public void initData() {
        _log.trace( Logging.LOG_LEVEL_INFO, "Controller.initData" );
    }

    public void initImages() {
        _imgs = new Image[IMG_COUNT];
        
        int count = 0;
        try{
            _imgs[IMG_IDX_DEFAULT_FONT] = Image.createImage( IMG_PATH_FONT ); count++;
            
            _imgs[IMG_IDX_BUTTON]               = Image.createImage( IMG_PATH_BUTTON ); count++;
            _imgs[IMG_IDX_BUTTON_DOWN]          = Image.createImage( IMG_PATH_BUTTON_DOWN ); count++;
            _imgs[IMG_IDX_BUTTON_SEL]           = Image.createImage( IMG_PATH_BUTTON_SEL ); count++;
            _imgs[IMG_IDX_BACKGROUND]           = Image.createImage( IMG_PATH_BACKGROUND ); count++;
            _imgs[IMG_IDX_HEADER]               = Image.createImage( IMG_PATH_HEADER ); count++;
            _imgs[IMG_IDX_CMD_BAR]              = Image.createImage( IMG_PATH_CMD_BAR ); count++;
            _imgs[IMG_IDX_CMD_PANEL]            = Image.createImage( IMG_PATH_CMD_PANEL ); count++;
            _imgs[IMG_IDX_CMD_HIGHLIGHT]        = Image.createImage( IMG_PATH_CMD_HIGHLIGHT ); count++;
            _imgs[IMG_IDX_TEXTBOX]              = Image.createImage( IMG_PATH_TEXTBOX ); count++;
            _imgs[IMG_IDX_TEXTBOX_SEL]          = Image.createImage( IMG_PATH_TEXTBOX_SEL ); count++;
            _imgs[IMG_IDX_DROPDOWN]             = Image.createImage( IMG_PATH_DROPDOWN ); count++;
            _imgs[IMG_IDX_DROPDOWN_SEL]         = Image.createImage( IMG_PATH_DROPDOWN_SEL ); count++;
            _imgs[IMG_IDX_DROPDOWN_PANEL]       = Image.createImage( IMG_PATH_DROPDOWN_PANEL ); count++;
            _imgs[IMG_IDX_DROPDOWN_HIGHLIGHT]   = Image.createImage( IMG_PATH_DROPDOWN_HL ); count++;
            _imgs[IMG_IDX_FONT_BIG_BLACK]       = Image.createImage( IMG_PATH_FONT_BIG_BLACK ); count++;
            _imgs[IMG_IDX_FONT_BIG_WHITE]       = Image.createImage( IMG_PATH_FONT_BIG_WHITE ); count++;
            _imgs[IMG_IDX_FONT_SMALL_BLACK]     = Image.createImage( IMG_PATH_FONT_SMALL_BLACK ); count++;
            _imgs[IMG_IDX_FONT_SMALL_WHITE]     = Image.createImage( IMG_PATH_FONT_SMALL_WHITE ); count++;
            _imgs[IMG_IDX_IMG_DIGITRIX]         = Image.createImage( IMG_PATH_DIGITRIX ); count++;
            _imgs[IMG_IDX_IMG_DIGITRIX_1]       = Image.createImage( IMG_PATH_DIGITRIX_1 ); count++;            
            
            _imgs[IMG_IDX_SPOONFED_LOGO]        = Image.createImage( IMG_PATH_SPOONFED_LOGO ); count++;
            _imgs[IMG_IDX_LEFT_TAB_ARROW]       = Image.createImage( IMG_PATH_LEFT_TAB_ARROW ); count++;
            _imgs[IMG_IDX_RIGHT_TAB_ARROW]      = Image.createImage( IMG_PATH_RIGHT_TAB_ARROW ); count++;
            _imgs[IMG_IDX_TAB_01_IMAGE]         = Image.createImage( IMG_PATH_TAB_01_IMAGE ); count++;
            _imgs[IMG_IDX_TAB_01_FOCUS_IMAGE]   = Image.createImage( IMG_PATH_TAB_01_FOCUS_IMAGE ); count++;
            _imgs[IMG_IDX_TAB_01_SEL_IMAGE]     = Image.createImage( IMG_PATH_TAB_01_SEL_IMAGE ); count++;
            _imgs[IMG_IDX_TAB_ICON_CL]          = Image.createImage( IMG_PATH_TAB_ICON_CL ); count++;
            _imgs[IMG_IDX_TAB_ICON_FD]          = Image.createImage( IMG_PATH_TAB_ICON_FD ); count++;
            _imgs[IMG_IDX_TAB_ICON_GP]          = Image.createImage( IMG_PATH_TAB_ICON_GP ); count++;
            _imgs[IMG_IDX_TAB_ICON_MG]          = Image.createImage( IMG_PATH_TAB_ICON_MG ); count++;
            _imgs[IMG_IDX_TAB_ICON_MG_SEL]      = Image.createImage( IMG_PATH_TAB_ICON_MG_SEL ); count++;
            _imgs[IMG_IDX_TAB_ICON_NL]          = Image.createImage( IMG_PATH_TAB_ICON_NL ); count++;
            _imgs[IMG_IDX_TAB_ICON_ST]          = Image.createImage( IMG_PATH_TAB_ICON_ST ); count++;
            _imgs[IMG_IDX_TAB_02_IMAGE]         = Image.createImage( IMG_PATH_TAB_02_IMAGE ); count++;
            _imgs[IMG_IDX_TAB_02_FOCUS_IMAGE]   = Image.createImage( IMG_PATH_TAB_02_FOCUS_IMAGE ); count++;
            _imgs[IMG_IDX_TAB_02_SEL_IMAGE]     = Image.createImage( IMG_PATH_TAB_02_SEL_IMAGE ); count++;
            
            _imgs[IMG_IDX_BUTTON_LONG]             = Image.createImage( IMG_PATH_BUTTON_LONG ); count++;
            _imgs[IMG_IDX_BUTTON_LONG_SEL]         = Image.createImage( IMG_PATH_BUTTON_LONG_SEL ); count++;
            _imgs[IMG_IDX_CATEGORY_ICON_BEER_FUN]  = Image.createImage( IMG_PATH_CATEGORY_ICON_BEER_FUN ); count++;
            _imgs[IMG_IDX_CATEGORY_ICON_COMEDY]    = Image.createImage( IMG_PATH_CATEGORY_ICON_COMEDY ); count++;
            _imgs[IMG_IDX_CATEGORY_ICON_DJ]        = Image.createImage( IMG_PATH_CATEGORY_ICON_DJ ); count++;
            _imgs[IMG_IDX_CATEGORY_ICON_EXHIBIT]   = Image.createImage( IMG_PATH_CATEGORY_ICON_EXHIBIT ); count++;
            _imgs[IMG_IDX_CATEGORY_ICON_FILMS]     = Image.createImage( IMG_PATH_CATEGORY_ICON_FILMS ); count++;
            _imgs[IMG_IDX_CATEGORY_ICON_LMUSIC]    = Image.createImage( IMG_PATH_CATEGORY_ICON_LMUSIC ); count++;
            _imgs[IMG_IDX_CATEGORY_ICON_THEATRE]   = Image.createImage( IMG_PATH_CATEGORY_ICON_THEATRE ); count++;
            
            _imgs[IMG_IDX_LIST_RADIO_ON]   = Image.createImage( IMG_PATH_LIST_RADIO_ON ); count++;
            _imgs[IMG_IDX_LIST_RADIO_OFF]  = Image.createImage( IMG_PATH_LIST_RADIO_OFF ); count++;
            
            _imgs[IMG_IDX_DIALOG]  = Image.createImage( IMG_PATH_DIALOG ); count++;
            _imgs[IMG_IDX_DIALOG_TITLE_GR]  = Image.createImage( IMG_PATH_DIALOG_TITLE_GR ); count++;
            _imgs[IMG_IDX_DIALOG_TITLE_SF]  = Image.createImage( IMG_PATH_DIALOG_TITLE_SF ); count++;
            _imgs[IMG_IDX_DIALOG_TITLE_WH]  = Image.createImage( IMG_PATH_DIALOG_TITLE_WH ); count++;
            
            _imgs[IMG_IDX_AJ_TEXTBOX_OFF]          = Image.createImage( IMG_PATH_AJ_TEXTBOX_OFF ); count++;
            _imgs[IMG_IDX_AJ_TEXTBOX_ON]           = Image.createImage( IMG_PATH_AJ_TEXTBOX_ON ); count++;
            _imgs[IMG_IDX_AJ_TEXTBOX_PANEL]        = Image.createImage( IMG_PATH_AJ_TEXTBOX_PANEL ); count++;
            _imgs[IMG_IDX_AJ_TEXTBOX_PANEL_HL]     = Image.createImage( IMG_PATH_AJ_TEXTBOX_PANEL_HL ); count++;
            
        } catch( IOException ioe ){
            _log.trace( Logging.LOG_LEVEL_ERRORS, "Controller.initImages; failed at point " + count );
        }
        
        _log.trace( Logging.LOG_LEVEL_INFO, "Controller.initImages - complete" );
    }

    public void initFonts() {
        // set up default font
        //CustomFont font = new CustomFont( FNT_DEFAULT_FONT );
        WFont font = new WFont();
        font.initFont( WFont.SIZE_BIG, Constants.COLOUR_BLACK, Constants.CHARACTER_MAPPING, _imgs[IMG_IDX_FONT_BIG_BLACK], 11, 13, false, Constants.BIG_CHAR_OFFSETS );
        font.setSpacing( 11 );
        
        initDefaultFont( font );
        
        _whiteFont = new WFont();
        _whiteFont.initFont( WFont.SIZE_BIG, Constants.COLOUR_WHITE, Constants.CHARACTER_MAPPING, _imgs[IMG_IDX_FONT_BIG_WHITE], 11, 13, false, Constants.BIG_CHAR_OFFSETS );
        _whiteFont.setSpacing( 11 );
        
        _smallBlackFont = new WFont();
        _smallBlackFont.initFont( WFont.SIZE_SMALL, Constants.COLOUR_BLACK, Constants.CHARACTER_MAPPING, _imgs[IMG_IDX_FONT_SMALL_BLACK], 10, 11, false, Constants.SMALL_CHAR_OFFSETS );
        _smallBlackFont.setSpacing( 11 );
        
        _smallWhiteFont = new WFont();
        _smallWhiteFont.initFont( WFont.SIZE_SMALL, Constants.COLOUR_WHITE, Constants.CHARACTER_MAPPING, _imgs[IMG_IDX_FONT_SMALL_WHITE], 10, 11, false, Constants.SMALL_CHAR_OFFSETS );
        _smallWhiteFont.setSpacing( 11 );
        
        _log.trace( Logging.LOG_LEVEL_INFO, "TestController.initFonts - complete" );
    }

    private TabView _mainMenu = null;
    
    public void initFirstPage() {
        _mainMenu = new TabView( 0, 0, gScreenWidth, gScreenHeight, 
                getDefaultFont(), _imgs[IMG_IDX_TAB_01_IMAGE], _imgs[IMG_IDX_TAB_01_FOCUS_IMAGE], _imgs[IMG_IDX_TAB_01_SEL_IMAGE], 
                _imgs[IMG_IDX_LEFT_TAB_ARROW], _imgs[IMG_IDX_RIGHT_TAB_ARROW], Constants.COLOUR_BLACK,  
                _imgs[IMG_IDX_HEADER],  _imgs[IMG_IDX_SPOONFED_LOGO], Constants.ALIGN_CENTRE, "Tab View", Constants.ALIGN_CENTRE );
        
        _mainMenu.setHeaderFont( _whiteFont );
        
        _mainMenu.setBG( _imgs[IMG_IDX_BACKGROUND] );
        
        _mainMenu.addTab( "tab1-parenttab", _imgs[IMG_IDX_TAB_ICON_MG], _imgs[IMG_IDX_TAB_ICON_MG_SEL], false, false );
        _mainMenu.addTab( "tab2", _imgs[IMG_IDX_TAB_ICON_CL], _imgs[IMG_IDX_TAB_ICON_CL] );
        _mainMenu.addTab( "tab3", _imgs[IMG_IDX_TAB_ICON_FD], _imgs[IMG_IDX_TAB_ICON_FD] );
        _mainMenu.addTab( "tab4", _imgs[IMG_IDX_TAB_ICON_GP], _imgs[IMG_IDX_TAB_ICON_GP] );
        _mainMenu.addTab( "tab5", _imgs[IMG_IDX_TAB_ICON_NL], _imgs[IMG_IDX_TAB_ICON_NL] );
        _mainMenu.addTab( "tab6", _imgs[IMG_IDX_TAB_ICON_ST], _imgs[IMG_IDX_TAB_ICON_ST] );
        
        // add some widgets to the tabs
        // tab1
//        _mainMenu.addWidget( getButton( "but1", "Button 1" ) );
//        _mainMenu.addWidget( getTextBox( "txt1", "Text Box" ) );
//        _mainMenu.addWidget( getDropDown( "ddl1", new String[]{"item 1", "item 2", "item 3"}, _mainMenu.getHeight() ) );
        _mainMenu.addWidget( getTestTabView() );
        
        // tab 2
        _mainMenu.addWidget( "tab2", getButton( "but2", "Button 2" ) );
        _mainMenu.addWidget( "tab2", getTextBox( "txt2", "Text Box 2" ) );
        _mainMenu.addWidget( "tab2", getDropDown( "ddl3", new String[]{"item 3", "item 4", "item 5"}, _mainMenu.getHeight() ) );
        
        // tab 3        
        _mainMenu.addWidget( 2, getTextBox( "txt2", "Text Box 2" ) );
        _mainMenu.addWidget( 2, getDropDown( "ddl3", new String[]{"item 3", "item 4", "item 5"}, _mainMenu.getHeight() ) );
        _mainMenu.addWidget( 2, getButton( "but3", "Button 2" ) );
        _mainMenu.addWidget( 2, getLabel( "label3", "My label", Constants.ALIGN_CENTRE ) );
        
        
        initCommandBar();
        
        //_cmdBar.initCommands( new int[]{CMD_TEST_1, CMD_TEST_2, CMD_TEST_3, CMD_TEST_4, CMD_TEST_5}, new int[]{CMD_TEST_5} );
        _cmdBar.initCommands( new int[]{CMD_TEST_1, CMD_TEST_2, CMD_TEST_3, CMD_TEST_4, CMD_TEST_5}, new int[]{CMD_TEST_3, CMD_TEST_4, CMD_TEST_5} );
        
        _mainMenu.setCommandBar( _cmdBar, Constants.ALIGN_BOTTOM );
        
        setNextView( "mainmenu", _mainMenu );               
        
        _log.trace( Logging.LOG_LEVEL_INFO, "TestController.initFirstPage - complete" );
        
        
    }
    
    private TabView getTestTabView(){
        /**
         * public TabView( int x, int y, int width, int height, WFont defaultFont, Image tabImage, Image tabFocusImage, Image selTabImage, 
            Image leftArrowImage, Image rightArrowImage, int bgTabColour ){
         */
        TabView tv = new TabView( 0, 0, gScreenWidth, _mainMenu.getViewableHeight(), 
                getDefaultFont(), _imgs[IMG_IDX_TAB_02_IMAGE], _imgs[IMG_IDX_TAB_02_FOCUS_IMAGE], _imgs[IMG_IDX_TAB_02_SEL_IMAGE], 
                _imgs[IMG_IDX_LEFT_TAB_ARROW], _imgs[IMG_IDX_RIGHT_TAB_ARROW], Constants.COLOUR_BLACK );
        
        tv.setHeaderFont( _whiteFont );
        
        // add some tabs to the main menu 
        
        tv.initTabPadding( 0, 10, 0, 0 );
        tv.addTab( "childtab-tab1", "Tab 1", false, true );
        tv.addTab( "childtab-tab2", "Tab 2" );
        tv.addTab( "childtab-tab3", "Tab 3" );
        
        String[] lu = new String[]{"Aaron", "Adam", "Alastair", "Alex", "Ali", "Allan", "Andy", "Andrew", 
            "Ben", "Bianca", "Brad", "Bret", "Brian", "Bryan", 
            "Callum", "Chals", "Chris", 
            "Dan", "Darrel", "David", "Debbie", "Danielle", "Debora", "Dee Jay", "Dennis", "Diego", "Donna", 
            "Emerey", "Emma", "Eva", 
            "Fragil", "Fraser", 
            "Gino", "Greg", 
            "Holly", 
            "Jadie"};
        
        WAjaxTextBox aTb = getAJTextBox("atb", lu, tv.getViewableHeight() );
        
        tv.addWidget( "childtab-tab1", aTb );
        
        ExpandableWidgetContainer widgets = new ExpandableWidgetContainer( tv.getTab("childtab-tab1").getWidgetContainer(), getLongButton( "but2", _imgs[IMG_IDX_CATEGORY_ICON_BEER_FUN] ), false, gScreenWidth );         
        widgets.setExpandOnFocus( true ); 
        widgets.setFont( getDefaultFont() );  
        widgets.addWidget( getScatteredList( "lst1", _smallBlackFont, _smallWhiteFont, 0xEC008C, 0x000000, widgets.getWidth(),
                new String[]{"item 1", "longer item 2", "item 3", "longer item 4", "longer item 5", "very long item 6", "item 7", "item 8"} ), false);
        tv.addWidget( "childtab-tab1", widgets );
        
        widgets = new ExpandableWidgetContainer( tv.getTab("childtab-tab1").getWidgetContainer(), getLongButton( "but2", _imgs[IMG_IDX_CATEGORY_ICON_COMEDY] ), false, gScreenWidth );         
        widgets.setExpandOnFocus( true ); 
        widgets.setFont( getDefaultFont() );  
        widgets.addWidget( getOrderedList( "lst2", _smallBlackFont, _smallBlackFont, _imgs[IMG_IDX_LIST_RADIO_OFF], _imgs[IMG_IDX_LIST_RADIO_ON], 0xEC008C, widgets.getWidth(),
                new String[]{"item 1", "item 2", "item 3", "item 4"} ), false);
        tv.addWidget( "childtab-tab1", widgets );        
        tv.addWidget( "childtab-tab2", getButton( "but2", "Button 2" ) );
        tv.addWidget( "childtab-tab2", getTextBox( "txt2", "Text Box 2" ) );
        tv.addWidget( "childtab-tab2", getDropDown( "ddl3", new String[]{"item 3", "item 4", "item 5"}, _mainMenu.getHeight() ) );
        
        tv.setAsChild();
        
        return tv; 
    }

    protected void cleanup() {
        _log.trace( Logging.LOG_LEVEL_INFO, "cleanup" );
    }

    public void handleKeyPressed(int action, int keyCode) {
        if( hasDialog() && getDialog().isVisible() ){
            if( getDialog().handleKeyPressed( action, keyCode ) )
                return; 
        }
        else if( _cmdBar != null ){
            if( _cmdBar.handleKeyPressed( action, keyCode ) )
                return;
        }
        
        if( getCurrentView() != null )
            getCurrentView().handleKeyPressed( action, keyCode );
    }

    public void handleKeyReleased(int action, int keyCode) {
        if( getCurrentView() != null )
            getCurrentView().handleKeyReleased( action, keyCode );
    }

    public void cycle() {
        if( _cmdBar != null ){
            if( isDialogShowing() )
                _cmdBar.enabled( false );
            else if( !_cmdBar.isEnabled() )
                _cmdBar.enabled( true );
            
            _cmdBar.cycle();
        }
    }

    public void handleEvent(Event evt) {
//        throw new UnsupportedOperationException("Not supported yet.");
    }

    public boolean isActiveSubscriber() {
        return false; 
//        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void setActiveSubscriber(boolean active) {
//        throw new UnsupportedOperationException("Not supported yet.");
    }
    
    //<editor-fold defaultstate="collapsed" desc="helper methods to add widgets">
    
    /** create a commandbar that will be used by the whole application **/
    private void initCommandBar(){
        StandardCmdBar cb = new StandardCmdBar( _imgs[IMG_IDX_CMD_BAR], _imgs[IMG_IDX_CMD_PANEL], _imgs[IMG_IDX_CMD_HIGHLIGHT],
                null, CMDS, CMD_LABELS, null, 0, 0, _smallBlackFont );
        
        cb.setID( "cmdBar" );
        cb.setCmdPanelDocking( StandardCmdBar.PANEL_DOCKING_CENTRE );
        
        cb.addEventSubscriber( Event.EVT_CMD_SELECTED, this );
        
        _cmdBar = cb;
    }
    
    private WList getOrderedList( String id, WFont font, WFont selectedFont, Image icon, Image selIcon, int focusedColour, int width, String[] items ){
        WList w = new WList( id );
        w.setWidth( width );        
        w.setFont( font );
        w.setSelectedFont( selectedFont );
        w.setIcons( icon, selIcon );
        w.setListLayout( WList.LAYOUT_ORDERED );
        w.setSelectionMode( WList.MODE_EXCLUSIVE );
        w.setFocusHighlightBgColour(focusedColour);
        
        if( items != null ){
            SimpleDataItem[] itms = new SimpleDataItem[items.length];
            for( int i=0; i<items.length; i++ ){
                itms[i] = new SimpleDataItem( items[i] ); 
            }
            
            w.setItems(itms);
        }
        
        return w; 
    }
    
    private WList getScatteredList( String id, WFont font, WFont selectedFont, int focusedHLColour, int selectedHLColour, int width, String[] items ){
        WList w = new WList( id );
        w.setWidth( width );
        w.setIncludeAllOption( true );
        w.setIncludeClearOption( true );
        w.setFont( font );
        w.setSelectedFont( selectedFont );
        w.setFocusHighlightBgColour( focusedHLColour );
        w.setSelectedHighlightBgColour(selectedHLColour);
        
        
        if( items != null ){
            SimpleDataItem[] itms = new SimpleDataItem[items.length];
            for( int i=0; i<items.length; i++ ){
                itms[i] = new SimpleDataItem( items[i] ); 
            }
            
            w.setItems(itms);
        }
        
        return w; 
    }
    /**
     * helper method used to create a button widget 
     * param id: id of the widget (button)
     * param label: label for the widget (button)
     **/
    private WButton getButton( String id, String label ){
        
        WButton but = new WButton( id, getDefaultFont(),
                _imgs[IMG_IDX_BUTTON], _imgs[IMG_IDX_BUTTON_SEL], _imgs[IMG_IDX_BUTTON_DOWN] );
        but.setText( label );
        but.setAlignment( Constants.ALIGN_CENTRE_H | Constants.ALIGN_CENTRE_V );
        but.addEventSubscriber( Event.EVT_SELECT_DOWN, this );
        
        return but;
    }
    
    private WButton getLongButton( String id, Image icon ){
        
        WButton but = new WButton( id, getDefaultFont(),
                _imgs[IMG_IDX_BUTTON_LONG], _imgs[IMG_IDX_BUTTON_LONG_SEL], _imgs[IMG_IDX_BUTTON_LONG_SEL] );
        but.setIcon( icon, 10, 0, Constants.ALIGN_LEFT | Constants.ALIGN_CENTRE_V );
        but.setAlignment( Constants.ALIGN_CENTRE_H | Constants.ALIGN_CENTRE_V );
        but.addEventSubscriber( Event.EVT_SELECT_DOWN, this );
        
        return but;
    }
    
    /**
     * helper method used to create a textbox widget
     * param container: the parent you would like to add the button to
     * param id: id of the widget (textbox)
     * param initialText: initial text for the widget (textbox)
     **/
    private WTextBox getTextBox( String id, String initialText ){
        WTextBox tb = new WTextBox( id, getDefaultFont(), _imgs[IMG_IDX_TEXTBOX], _imgs[IMG_IDX_TEXTBOX_SEL] );
        tb.setText( initialText );
        tb.setAlignment( Constants.ALIGN_CENTRE_V | Constants.ALIGN_LEFT );
        
        return tb;
    }
    
    private WAjaxTextBox getAJTextBox( String id, String[] dictionary, int viewableHeight ){
        WAjaxTextBox tb = new WAjaxTextBox( id, getDefaultFont(), _imgs[IMG_IDX_AJ_TEXTBOX_OFF], _imgs[IMG_IDX_AJ_TEXTBOX_ON], 
               _imgs[IMG_IDX_AJ_TEXTBOX_PANEL], _imgs[IMG_IDX_AJ_TEXTBOX_PANEL_HL], viewableHeight );
        tb.setText( "" );
        tb.setLookupItems( dictionary );
        tb.setAlignment( Constants.ALIGN_CENTRE_V | Constants.ALIGN_LEFT );
        
        return tb;
    }
    
    /**
     * same as the private void addDropDown( String id, IDataItem[] options ) method but
     * autmatically converts an array of Strings to IDataItems (as SimpleDataItems)
     **/
    private WDropDown getDropDown( String id, String[] options, int viewHeight ){
        return getDropDown( id, MiscUtil.getInstance().convertToDataItem( options ), viewHeight );
    }
    
    /**
     * helper method used to create a dropdown widget 
     * param id: id of the widget (textbox)
     * param options: available options for the user to select from the drop down list
     * param viewHeight: height of the container you're adding the widget to 
     **/
    private WDropDown getDropDown( String id, IDataItem[] options, int viewHeight ){
        WDropDown ddl = new WDropDown( id, _imgs[IMG_IDX_DROPDOWN], _imgs[IMG_IDX_DROPDOWN_SEL],
                _imgs[IMG_IDX_DROPDOWN_PANEL], _imgs[IMG_IDX_DROPDOWN_HIGHLIGHT], getDefaultFont(), viewHeight );
        
        ddl.setItems( options );
        
        return ddl;
    }
    
    /**
     *
     **/
    private WLabel getLabel( String id, String text, int alignment  ){
        WLabel label = new WLabel( id, getDefaultFont(), text, alignment );
        return label;
    }
    
    /**
     *
     **/
    private WLabel getLabel( String id, Image img, int alignment ){
        WLabel label = new WLabel( id, img, alignment );
        return label;
    }
    
    private DialogView getDialog( String id, Image dialog, Image title ){
        int x = ((gScreenWidth-dialog.getWidth())>>1);
        int y = ((gScreenHeight-dialog.getHeight())>>1);
        DialogView dv = new DialogView(x, y, 
                dialog.getWidth(), dialog.getHeight(), 
                getDefaultFont(), this );
        
        dv.setDialogImage( dialog, 34, 26, 56, 26 );
        
        dv.setTitle( title, Constants.ALIGN_LEFT, Constants.ALIGN_CENTRE_V, 
                10, 2 );
        
        return dv; 
    }
    
    //</editor-fold>
    
}
